package keeto.com.vn.classified.adapter;

import java.util.Hashtable;
import java.util.Vector;

import keeto.com.vn.classfied.model.Category;
import keeto.com.vn.classified.common.ICategoriesProductsAdapter;
import keeto.com.vn.classified.ui.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class CategoriesAdapter extends BaseAdapter implements ICategoriesProductsAdapter{
	public static final String ACTION_SELECT_SEARCH = "SelectForSearch";
	public static final String CMD_SEARCH_PRODUCT = "SelectForSearchProduct";
	public static final String ACTION_SPECIAL = "SelectAllSpecial";

	private static Hashtable<String, Integer> hstbIcons = new Hashtable<String, Integer>();
	
	static {
	}

	class ViewHolder {
		private TextView tvCategory;
		private ImageView ivIcon;
	}

	private static final String TAG = "CategoryAdapter";
	/**
	 * @category 数据源
	 * */
	private Vector<Category> dataSource;
	private Context context;
	/**
	 * @category 何种操作
	 * */
	private String whatAction;

	public CategoriesAdapter(Vector<Category> dataSource, Context context,
			String action) {
		this.dataSource = dataSource;
		this.context = context;
		this.whatAction = action;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void setDataSource(Vector<?> dataSource) {
		this.dataSource = (Vector<Category>) dataSource;
	}

	@Override
	public int getCount() {
		if (dataSource == null || dataSource.size() == 0) {
			return 0;
		} else {
			return dataSource.size();
		}
	}

	@Override
	public Object getItem(int position) {
		return dataSource.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (dataSource.get(position).getRemoteId().equals("3")
				|| dataSource.get(position).getRemoteId().equals("8")
				|| dataSource.get(position).getRemoteId().equals("10")
				|| dataSource.get(position).getRemoteId().equals("13")){
		}
		if (dataSource == null || dataSource.size() == 0) {
			return null;
		} else {
				Category category = dataSource.get(position);
				int count = dataSource.get(position).getCount();
				count = count < 0 ? 0 : count;
				ViewHolder holder = null;
				View view = convertView;
				if (view != null && view.getTag() != null) {
					holder = (ViewHolder) view.getTag();
				} else {
					view = LayoutInflater.from(context).inflate(R.layout.ia_category_item_view, parent, false);
					holder = new ViewHolder();
					holder.tvCategory = (TextView) view.findViewById(R.id.tv_name);
					holder.ivIcon = (ImageView) view.findViewById(R.id.icon);
					view.setTag(holder);
				}
				if (hstbIcons.containsKey(category.getRemoteId())) {
					holder.ivIcon.setVisibility(View.VISIBLE);
					holder.ivIcon.setImageResource(hstbIcons.get(category.getRemoteId()).intValue());
				}
				if (whatAction.equals(CMD_SEARCH_PRODUCT)|| whatAction.equals(ACTION_SPECIAL)) {
					holder.tvCategory.setText(category.getName() + "("+ String.valueOf(count) + ")");
				} else {
					holder.tvCategory.setText(category.getName());
				}
				return view;
		}
	}
	
}
